using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace zeroflag.Windows.Custom
{
	public class RenderArea
	{
		Graphics m_Graphics;

		public Graphics Graphics
		{
			get { return m_Graphics; }
			set { m_Graphics = value; }
		}

		Control m_RenderTarget;

		public Control RenderTarget
		{
			get { return m_RenderTarget; }
			set { m_RenderTarget = value; }
		}

		int Left;
		int Top;
		int Width;
		int Height;

		public RenderArea(RenderArea from)
			: this(from.Graphics, from.RenderTarget)
		{
		}

		public RenderArea(Graphics g, Control renderTarget)
		{
			this.m_Graphics = g;
			this.m_RenderTarget = renderTarget;
			this.Left = renderTarget.Left;
			this.Top = renderTarget.Top;
			this.Width = 0;
			this.Height = 0;
		}

		public RenderArea Include(Rectangle area)
		{
			return this.Include(area.X + area.Width, area.Y + area.Height);
		}
		public RenderArea Include(Point p)
		{
			return this.Include(p.X, p.Y);
		}
		public RenderArea Include(Size area)
		{
			return this.Include(area.Width, area.Height);
		}
		public RenderArea Include(int x, int y)
		{
			if (this.Top + this.Width < y)
			{
				this.Width = y - this.Top;
			}
			if (this.Left + this.Height < x)
			{
				this.Height = x - this.Left;
			}
			return this;
		}

		public static RenderArea operator +(RenderArea area, Rectangle rect)
		{
			return new RenderArea(area).Include(rect);
		}
		public static RenderArea operator +(RenderArea area, Point p)
		{
			return new RenderArea(area).Include(p);
		}
		public static RenderArea operator +(RenderArea area, Size size)
		{
			return new RenderArea(area).Include(size);
		}

		public bool Apply()
		{
			bool changes = false;
			if (this.RenderTarget.Left != this.Left)
			{
				this.RenderTarget.Left = this.Left;
				changes = true;
			}
			if (this.RenderTarget.Top != this.Top)
			{
				this.RenderTarget.Top = this.Top;
				changes = true;
			}
			if (this.RenderTarget.Width != this.Width)
			{
				this.RenderTarget.Width = this.Width + 8;
				changes = true;
			}
			if (this.RenderTarget.Height != this.Height)
			{
				this.RenderTarget.Height = this.Height + 8;
				changes = true;
			}
			return changes;
		}

		public override string ToString()
		{
			return "[" + this.Left + "/" + this.Top + "][" + this.Width + "/" + this.Height + "]";//base.ToString();
		}
	}
}
